package com.zzyl.service.impl;

import cn.hutool.json.JSONUtil;
import com.zzyl.constant.CacheConstants;
import com.zzyl.service.RedisService;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.DeviceDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @Description RedisServiceImpl
 * @Author Hua Caoxing
 * @Data 2025-09-15
 */
@Service
public class RedisServiceImpl implements RedisService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;


    /**
     * 获取缓存的数据
     *
     * @return
     */
    @Override
    public Map<String, List<DeviceDataVo>> getAllLatestDeviceData() {
        // 获取 小key和值
        Map<Object, Object> entries = redisTemplate.opsForHash().entries(CacheConstants.DEVICE_LASTDATA_CACHE_KEY);
        // 转换为 DeviceDataVo 列表
        Map<String, List<DeviceDataVo>> maps = new HashMap<>();
        for (Map.Entry<Object, Object> entry : entries.entrySet()) {
            String value = (String) entry.getValue();
            List<DeviceDataVo> deviceDataVo = JSONUtil.toList(value, DeviceDataVo.class);
            //过滤时间产生在一分钟之内的数据
            List<DeviceDataVo> collect = deviceDataVo.stream().filter(d -> d.getAlarmTime().plusMinutes(1).isAfter(LocalDateTime.now())).collect(Collectors.toList());
            maps.put((String) entry.getKey(), collect);
        }
        return maps;
    }

    /**
     * @param iotId
     * @param functionId
     * @param alertCountCacheKey
     */
    @Override
    public void delete(String iotId, String functionId, String alertCountCacheKey) {
        redisTemplate.opsForValue().set(alertCountCacheKey + ":" + iotId + "-" + functionId, "0");
    }


    /**
     * 设置报警静默时间
     *
     * @param alertSilentPeriod
     * @param iotId
     * @param functionId
     * @param
     * @param
     */
    @Override
    public void setSilentPeriod(String iotId, String functionId, Integer alertSilentPeriod) {
        // 设置过期时间
        redisTemplate.opsForValue()
                .set(CacheConstants.ALERT_SILENT_PERIOD + ":" + iotId + "-" + functionId, String.valueOf(alertSilentPeriod), alertSilentPeriod, TimeUnit.MINUTES);

    }

    /**
     * 设置报警次数
     *
     * @param alertCountCacheKey
     * @param iotId
     * @param functionId
     * @param count
     */
    @Override
    public void setCount(String alertCountCacheKey, String iotId, String functionId, Integer count) {
        redisTemplate.opsForValue().set(alertCountCacheKey + ":" + iotId + "-" + functionId, String.valueOf(count));
    }

    /**
     * 键获取值
     * @param alertCountCacheKey
     * @param iotId
     * @param functionId
     * @return
     */
    @Override
    public String hasKey(String alertCountCacheKey, String iotId, String functionId) {
        String result = redisTemplate.opsForValue().get(alertCountCacheKey + ":" + iotId + "-" + functionId);
        return result;
    }

}
